package com.kaluli.project.utils;

import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.annotation.adapters.HexBinaryAdapter;


/*
 * 返回SHA512加密字符串
 */
public class SHA512Util {
	

	private static byte[] initHmacSHA512Key() throws NoSuchAlgorithmException {
		// 初始化HmacMD5摘要算法的密钥产生器
		KeyGenerator generator = KeyGenerator.getInstance("HmacSHA512");
		// 产生密钥
		SecretKey secretKey = generator.generateKey();
		// 获得密钥
		byte[] key = secretKey.getEncoded();
		return key;
	}

	private static String encodeHmacSHA512(byte[] data, byte[] key) throws Exception {
		// 还原密钥
		SecretKey secretKey = new SecretKeySpec(key, "HmacSHA512");
		// 实例化Mac
		Mac mac = Mac.getInstance(secretKey.getAlgorithm());
		// 初始化mac
		mac.init(secretKey);
		// 执行消息摘要
		byte[] digest = mac.doFinal(data);
		return new HexBinaryAdapter().marshal(digest);// 转为十六进制的字符串
	}

	public static String SHA512Encode(String data) throws Exception {

		byte[] keyHmacSHA512 = SHA512Util.initHmacSHA512Key();
		return (SHA512Util.encodeHmacSHA512(data.getBytes(), keyHmacSHA512));
	}


	private static final String HMAC_SHA512 = "HmacSHA512";

	/**
	 * 
	 * @param data
	 * @param key
	 * @return
	 * @throws Exception
	 */
	public static String getSignature(String data, String key) throws Exception {
		byte[] keyBytes = key.getBytes();
		SecretKeySpec signingKey = new SecretKeySpec(keyBytes, HMAC_SHA512);
		Mac mac = Mac.getInstance(HMAC_SHA512);
		mac.init(signingKey);
		byte[] rawHmac = mac.doFinal(data.getBytes());
		StringBuilder sb = new StringBuilder();
		sb.append(new HexBinaryAdapter().marshal(rawHmac));
		return sb.toString();
	}

	public static void main(String[] args) throws Exception {
		String accessKey = "7be25652-49ab-4dbd-bccd-6e5c92604d55";
		
		String skey = "807A3B29AC786EE76875240895029946114D8E9794B158C151CB6177C702BE360E1A97710C5D3A72BDDD8517FA8A499C350939DF3E89E5429B2B3349DC0DD354";
		
		String requestBody = "{'enOrderCode':'123123','wayBillNo':'123123123','logisticsname':'123123123','notes':'sadfasdf'}";
		Long timestamp = System.currentTimeMillis();
		
		Map<String, String> paramMap = new HashMap<String, String>();  
		paramMap.put("requestBody", requestBody);
		paramMap.put("timestamp", timestamp.toString());
		
		String[] keyArray = paramMap.keySet().toArray(new String[0]);  
		Arrays.sort(keyArray);  
		  
		// 拼接有序的参数名-值串  
		StringBuilder stringBuilder = new StringBuilder();  
		stringBuilder.append(accessKey);  
		for (String key : keyArray)  
		{  
		    stringBuilder.append(key).append(paramMap.get(key));  
		}  
		  
		stringBuilder.append(skey);  
		String sign = SHA512Util.getSignature(stringBuilder.toString(), skey); 
		StringBuffer params =new StringBuffer();
		params.append("requestBody=").append(requestBody).append("&timestamp=").append(timestamp).append("&sign=111").append(sign);
	}
}
